package com.jincp.image.pick.ui.file;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.android.internal.util.Predicate;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jincp.image.pick.DocScannerTask;
import com.jincp.image.pick.DocumentPicker;
import com.jincp.image.pick.FileBaseActivity;
import com.jincp.image.R;
import com.jincp.image.pick.Utils;
import com.jincp.image.pick.adapter.file.SectionsPagerAdapter;
import com.jincp.image.pick.bean.Document;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 视频的加载界面   注意 视频选择不需要剪裁
 */
public class FileViewPagerActivity extends FileBaseActivity {

    public static final int REQUEST_CODE = 0x00200;

    private static final String TAG = FileViewPagerActivity.class.getSimpleName();

    public static void start(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, FileViewPagerActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void start(Fragment fragment, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), FileViewPagerActivity.class);
        fragment.startActivityForResult(intent, requestCode);
    }

    public static final String PDF_FRAGMENT = "PDF";
    public static final String PPT_FRAGMENT = "PPT";
    public static final String WORD_FRAGMENT = "DOC";
    public static final String EXCEL_FRAGMENT = "XLS";
    public static final String TXT_FRAGMENT = "TXT";

    private Button mBtnOk;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ProgressBar progressBar;

    private DocumentPicker documentPicker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doc_picker);
        setToolBar(R.id.toolbar, new ToolBarOptions("选择文件"));
        initViews();
        initData();
    }

    private void initViews() {
        mBtnOk = (Button) findViewById(R.id.btn_ok);
        mBtnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(DocumentPicker.EXTRA_DOCUMENT_ITEMS, documentPicker.getSelectedDocuments());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);

    }

    private void initData() {
        documentPicker = DocumentPicker.getInstance();
        new DocScannerTask(this, new DocScannerTask.OnDocumentLoadedListener() {
            @Override
            public void onDocumentLoaded(List<Document> files) {
                progressBar.setVisibility(View.GONE);
                setDataOnFragments(files);
            }
        }).execute();
        setUpViewPager();
    }

    private void setDataOnFragments(List<Document> files) {
        SectionsPagerAdapter sectionsPagerAdapter = (SectionsPagerAdapter) viewPager.getAdapter();
        if (sectionsPagerAdapter != null) {
            for (int index = 0; index < sectionsPagerAdapter.getCount(); index++) {
                DocFragment docFragment = (DocFragment) sectionsPagerAdapter.getItem(index);
                if (docFragment != null) {
                    if (index == 0)
                        docFragment.updateList(filterDocuments(Document.FILE_TYPE.PDF, files));
                    else if (index == 1)
                        docFragment.updateList(filterDocuments(Document.FILE_TYPE.PPT, files));
                    else if (index == 2)
                        docFragment.updateList(filterDocuments(Document.FILE_TYPE.WORD, files));
                    else if (index == 3)
                        docFragment.updateList(filterDocuments(Document.FILE_TYPE.EXCEL, files));
                    else if (index == 4)
                        docFragment.updateList(filterDocuments(Document.FILE_TYPE.TXT, files));
                }
            }
        }
    }

    private void setUpViewPager() {
        SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(DocFragment.newInstance(documentPicker.getSelectedPaths()), PDF_FRAGMENT);
        adapter.addFragment(DocFragment.newInstance(documentPicker.getSelectedPaths()), PPT_FRAGMENT);
        adapter.addFragment(DocFragment.newInstance(documentPicker.getSelectedPaths()), WORD_FRAGMENT);
        adapter.addFragment(DocFragment.newInstance(documentPicker.getSelectedPaths()), EXCEL_FRAGMENT);
        adapter.addFragment(DocFragment.newInstance(documentPicker.getSelectedPaths()), TXT_FRAGMENT);

        viewPager.setOffscreenPageLimit(5);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private ArrayList<Document> filterDocuments(final int type, List<Document> documents) {
        final Predicate<Document> docType = new Predicate<Document>() {
            public boolean apply(Document document) {
                return document.isThisType(type);
            }
        };

        return new ArrayList<>(Utils.filter(new HashSet<>(documents), docType));
    }
}